package com.exam.system.service.impl;

import com.exam.system.dao.ExamPaperDao;
import com.exam.system.dao.ExamPaperQuestionDao;
import com.exam.system.entity.ExamPaper;
import com.exam.system.entity.ExamUser;
import com.exam.system.entity.PageDTO;
import com.exam.system.redis.RedisUtils;
import com.exam.system.service.ExamPaperQuestionService;
import com.exam.system.service.ExamPaperService;
import com.exam.system.service.ExamUserService;
import com.exam.system.utils.JwtUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ExamPaperServiceImpl
 * @Description 试卷管理业务层
 * @Author ckl
 * @DATE 2021/5/31 17:44
 * @versions 1.0
 */
@Service
@Transactional
public class ExamPaperServiceImpl implements ExamPaperService {
    @Autowired
    private ExamPaperDao examPaperDao;
    @Autowired
    private ExamPaperQuestionService examPaperQuestionService;
    @Autowired
    private ExamUserService examUserService;
    @Autowired
    private RedisUtils redisUtils;

    /**
     * 添加试卷
     * @param examPaper 传进来要添加的试卷对象
     */
    @Override
    public void addPaper(ExamPaper examPaper, List<Integer> quesIdList, ServletRequest request) {
        // 获取当前登陆人信息
        String token = ((HttpServletRequest)request).getHeader("Authorization");
        // 数据补全,如果id为空说明是添加方法
        if (examPaper.getId() == null) {
            examPaper.setCreatorId(examUserService.login(JwtUtils.getUserName(token)).getId());
            examPaper.setCreateTime(new Date());
            examPaper.setIsdel("n");
            // 调用添加方法
            examPaperDao.addPaper(examPaper);
        } else {
            // 否则就是修改方法，修改需要删除之前试卷问题中间表的数据重新添加一次
            examPaperQuestionService.delExamPaperQuestionByPaperId(examPaper.getId());
            // 修改试卷基本信息
            examPaperDao.updatePaper(examPaper);
            // 修改人的信息
            examPaper.setUpdateTime(new Date());
        }
        // 添加关联表数据
        examPaperQuestionService.addExamPaperQuestionByQuesId(examPaper.getId(), quesIdList);
    }

    /**
     * 多条件查询所有
     * @param rbacPermDTO 分页中间类
     * @return 返回分页后的对象
     */
    @Override
    public PageInfo<Map<String, String>> findAllPaper(PageDTO<ExamPaper> paperPageDTO) {
        // 开启分页
        PageHelper.startPage(paperPageDTO.getCurrentPage(), paperPageDTO.getPageSize());
        // 查询分页后所有的试卷列表
        List<Map<String, String>> paperList = examPaperDao.findAllPaper(paperPageDTO.getEnt());
        // 返回分页对象
        return new PageInfo<>(paperList);
    }

    /**
     * 删除试卷
     * @param id
     * @return
     */
    @Override
    public void delPaperById(Integer id) {
        ExamPaper examPaper = new ExamPaper();
        examPaper.setId(id);
        examPaper.setIsdel("y");
        examPaperDao.updatePaper(examPaper);
    }


}
